package com.stu.management.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.stu.management.mapper.MiddleMapper;
import com.stu.management.mapper.RoleStuMapper;
import com.stu.management.pojo.Middle;
import com.stu.management.pojo.RoleStu;
import com.stu.management.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleStuMapper roleDao;
    @Autowired
    private MiddleMapper middleDao;

    @Override
    public List<RoleStu> getRoleList(int pageIndex, int pageSize) {
        PageHelper.startPage(pageIndex,pageSize);
        List<RoleStu> roleStus = roleDao.selectByExample(null);
        roleStus.forEach(c-> System.out.println(c));
        PageInfo<RoleStu> roleStuPageInfo = new PageInfo<>(roleStus);
        System.out.println("分页信息如下：");
        System.out.println("当前页:"+roleStuPageInfo.getPageNum());
        System.out.println("总页数:"+roleStuPageInfo.getPages());
        System.out.println("前一页:"+roleStuPageInfo.getPrePage());
        System.out.println("后一页:"+roleStuPageInfo.getNextPage());
        System.out.println("navigatepageNums:"+roleStuPageInfo.getNavigatepageNums());
        return roleStus;


    }

    @Override
    public int total() {
        return (int) roleDao.countByExample(null);
    }

    @Override
    public int insertRole(String rolename, String state, String[] ids) {
        int k1 = 0;
        try {
            //1.新增角色表
            RoleStu role = new RoleStu();
            role.setRolename(rolename);
            role.setRolestate(Integer.parseInt(state));
            int k = roleDao.insert(role);
            //2.新增中间表
            //如何获得新增数据的id?
            for (String s :
                    ids) {
                Middle middle = new Middle();
                middle.setRoleid(k);
                middle.setMenuid(Integer.valueOf(s));
                middleDao.insert(middle);
            }
            k1 = 1;
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return k1;
    }

    @Override
    public RoleStu findbyid(int roleid) {
        return roleDao.selectByPrimaryKey(roleid);
    }

    @Override
    public int updateById(RoleStu role, String[] ids) {
        try {
            int deleteMiddleNum = middleDao.deleteByPrimaryKey(role.getRoleid());
            int middleInsertNum = 0;
            if (deleteMiddleNum >= 0) {
                System.out.println("成功删除原有middle");
                for (String s :
                        ids) {
                    Middle middle = new Middle();
                    middle.setRoleid(role.getRoleid());
                    middle.setMenuid(Integer.valueOf(s));
                    if (middleDao.insert(middle)>0) {
                        middleInsertNum++;
                    }
                }
                if (middleInsertNum > 0) {
                    System.out.println("成功添加现有middle");
                    return roleDao.updateByPrimaryKey(role);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int delete(int roleId) {
        return roleDao.deleteByPrimaryKey(roleId);
    }

    @Override
    public int enableRole(int roleid) {
        RoleStu roleStu = roleDao.selectByPrimaryKey(roleid);
        roleStu.setRolestate(1);
        return roleDao.updateByPrimaryKey(roleStu);
    }


}
